# =========================
# Appendix E — Ideologically Inconsistent Respondents
# =========================

# ---- Packages ----
suppressPackageStartupMessages({
  library(tidyverse)
  library(haven)
  library(cregg)     # mm(), plot.mm()
  library(ggplot2)
  library(ggeasy)
})

# ---- Directories (replication) ----
base_dir <- "~/Dropbox/Issue voting Chile"
data_dir <- file.path(base_dir, "09_replication/data")   # not used here but kept for consistency
fig_dir  <- file.path(base_dir, "09_replication/output")
dir.create(fig_dir, showWarnings = FALSE, recursive = TRUE)

# ---- Load data (Appendix E dataset) ----
# Note: Appendix E uses the "defiers" file from clean_data.
wave2_conjoint <- read_dta(file.path(base_dir, "01_data/clean_data/wave2_defiers.dta"))

# =========================
# Common recodes and labels (match Appendix D style)
# =========================

atr <- c("a_1","a_2","a_3","a_4","a_5","a_6")
wave2_conjoint[atr] <- lapply(wave2_conjoint[atr], factor)

wave2_conjoint <- wave2_conjoint %>%
  mutate(
    Ideology   = a_1,
    Gender     = a_2,
    Age        = a_3,
    Feminism   = a_4,
    Immigration= a_5,
    Crime      = a_6
  )

# Baseline reference levels as in D
wave2_conjoint$Ideology   <- factor(wave2_conjoint$Ideology, levels = c("Right","Left"))
wave2_conjoint$Feminism   <- factor(wave2_conjoint$Feminism, levels = c("Non-Feminist","Feminist"))
wave2_conjoint$Crime      <- factor(wave2_conjoint$Crime, levels = c("No Harsher Punishment","Harsher Punishment"))
wave2_conjoint$Immigration<- factor(wave2_conjoint$Immigration, levels = c("No Restrictions","New Immigration Restrictions"))

# Cosmetic renames to match captions
wave2_conjoint <- wave2_conjoint %>%
  mutate(
    Immigration = fct_recode(Immigration, "New Restrictions" = "New Immigration Restrictions"),
    Crime       = fct_recode(Crime,
                             "Less Punitive" = "No Harsher Punishment",
                             "More Punitive" = "Harsher Punishment")
  )

# ---- Plot styling helper (same aesthetic across E1–E4) ----
mm_style <- function(g, ttl) {
  g +
    theme_bw() +
    scale_color_manual(values = rep("black", 9)) +
    guides(color = "none") +
    theme(
      axis.text.x = element_text(size = 10, color = "black"),
      axis.text.y = element_text(size = 10, color = "black"),
      axis.title.x = element_blank()
    ) +
    scale_x_continuous(limits = c(0, 1), breaks = seq(0, 1, .2)) +
    labs(title = ttl) +
    easy_center_title() +
    theme(
      plot.title = element_text(size = 16),
      axis.text.x = element_text(size = 14),
      axis.text.y = element_text(size = 14)
    )
}

# =========================
# Figure E1 — Immigration inconsistent: non‑interacted marginal means
# (Left anti‑immigration & Right pro‑immigration; show Ideology and Immigration)
# =========================

# Rightists who are pro‑immigration (derecha_pro_mig == 1)
right_pro_mig <- subset(wave2_conjoint, derecha_pro_mig == 1)

mm_E1_right <- mm(
  right_pro_mig,
  choice_clean2 ~ Ideology + Gender + Age + Feminism + Immigration + Crime,
  id = ~ numericalid
)

# Keep the four Ideology×Immigration rows in the plotted object (same indices as your working code)
p_E1_right <- plot(mm_E1_right[c(1,2,10,11), ], vline = 0.5) +
  scale_y_discrete(labels = c(
    "Left_No Restrictions"  = "Left and No New Restrictions",
    "Left_New Restrictions" = "Left and New Restrictions",
    "Right_No Restrictions" = "Right and No Restrictions",
    "Right_New Restrictions"= "Right and New Restrictions"
  ))

fig_E1_right <- mm_style(p_E1_right, "Right Pro‑Immigration")
ggsave(file.path(fig_dir, "E1_right_pro_immigration_noninteracted.png"),
       fig_E1_right, width = 5, height = 5.4, dpi = 300)

# Leftists who are anti‑immigration (left_anti_mig == 1)
left_anti_mig <- subset(wave2_conjoint, left_anti_mig == 1)

mm_E1_left <- mm(
  left_anti_mig,
  choice_clean2 ~ Ideology + Gender + Age + Feminism + Immigration + Crime,
  id = ~ numericalid
)

p_E1_left <- plot(mm_E1_left[c(1,2,10,11), ], vline = 0.5) +
  scale_y_discrete(labels = c(
    "Left_No Restrictions"  = "Left and No New Restrictions",
    "Left_New Restrictions" = "Left and New Restrictions",
    "Right_No Restrictions" = "Right and No Restrictions",
    "Right_New Restrictions"= "Right and New Restrictions"
  ))

fig_E1_left <- mm_style(p_E1_left, "Left Anti‑Immigration")
ggsave(file.path(fig_dir, "E1_left_anti_immigration_noninteracted.png"),
       fig_E1_left, width = 5, height = 5.4, dpi = 300)

# =========================
# Figure E2 — Immigration inconsistent: interacted (Ideology * Immigration)
# =========================

# Right Pro‑Immigration: build combined profile for interaction
right_pro_mig2 <- right_pro_mig %>%
  mutate(profile1 = interaction(Ideology, Immigration, sep = "_"))

mm_E2_right <- mm(
  right_pro_mig2,
  choice_clean2 ~ profile1 + Ideology + Gender + Age + Feminism + Immigration + Crime,
  id = ~ numericalid
)
mm_E2_right$feature <- ifelse(mm_E2_right$feature == "profile1",
                              "Ideology*Immigration", mm_E2_right$feature)

p_E2_right <- plot(mm_E2_right[c(1,3,2,4), ], vline = 0.5) +
  scale_y_discrete(labels = c(
    "Left_No Restrictions"  = "Left and No New Restrictions",
    "Left_New Restrictions" = "Left and New Restrictions",
    "Right_No Restrictions" = "Right and No Restrictions",
    "Right_New Restrictions"= "Right and New Restrictions"
  ))

fig_E2_right <- mm_style(p_E2_right, "Right Pro‑Immigration (Interaction)")
ggsave(file.path(fig_dir, "E2_right_pro_immigration_interacted.png"),
       fig_E2_right, width = 5, height = 5.4, dpi = 300)

# Left Anti‑Immigration: interacted
left_anti_mig2 <- left_anti_mig %>%
  mutate(profile1 = interaction(Ideology, Immigration, sep = "_"))

mm_E2_left <- mm(
  left_anti_mig2,
  choice_clean2 ~ profile1 + Ideology + Gender + Age + Feminism + Immigration + Crime,
  id = ~ numericalid
)
mm_E2_left$feature <- ifelse(mm_E2_left$feature == "profile1",
                             "Ideology*Immigration", mm_E2_left$feature)

p_E2_left <- plot(mm_E2_left[c(1,3,2,4), ], vline = 0.5) +
  scale_y_discrete(labels = c(
    "Left_No Restrictions"  = "Left and No New Restrictions",
    "Left_New Restrictions" = "Left and New Restrictions",
    "Right_No Restrictions" = "Right and No Restrictions",
    "Right_New Restrictions"= "Right and New Restrictions"
  ))

fig_E2_left <- mm_style(p_E2_left, "Left Anti‑Immigration (Interaction)")
ggsave(file.path(fig_dir, "E2_left_anti_immigration_interacted.png"),
       fig_E2_left, width = 5, height = 5.4, dpi = 300)

# =========================
# Figure E3 — Feminism inconsistent: non‑interacted marginal means
# (Right Feminists & Left Anti‑Feminists; show Ideology and Feminism)
# =========================

# Rightists who are feminists (derecha_fem == 1)
right_feminist <- subset(wave2_conjoint, derecha_fem == 1)

mm_E3_right <- mm(
  right_feminist,
  choice_clean2 ~ Ideology + Gender + Age + Feminism + Immigration + Crime,
  id = ~ numericalid
)

# Indices (1,2,8,9) match the Ideology and Feminism rows from your working code
p_E3_right <- plot(mm_E3_right[c(1,2,8,9), ], vline = 0.5) +
  scale_y_discrete(labels = c(
    "Left_Non-Feminist" = "Left and Non‑Feminist",
    "Left_Feminist"     = "Left and Feminist",
    "Right_Non-Feminist"= "Right and Non‑Feminist",
    "Right_Feminist"    = "Right and Feminist"
  ))

fig_E3_right <- mm_style(p_E3_right, "Right Feminist")
ggsave(file.path(fig_dir, "E3_right_feminist_noninteracted.png"),
       fig_E3_right, width = 5, height = 5.4, dpi = 300)

# Leftists who are anti‑feminist (left_anti_fem == 1)
left_anti_fem <- subset(wave2_conjoint, left_anti_fem == 1)

mm_E3_left <- mm(
  left_anti_fem,
  choice_clean2 ~ Ideology + Gender + Age + Feminism + Immigration + Crime,
  id = ~ numericalid
)

p_E3_left <- plot(mm_E3_left[c(1,2,8,9), ], vline = 0.5) +
  scale_y_discrete(labels = c(
    "Left_Non-Feminist" = "Left and Non‑Feminist",
    "Left_Feminist"     = "Left and Feminist",
    "Right_Non-Feminist"= "Right and Non‑Feminist",
    "Right_Feminist"    = "Right and Feminist"
  ))

fig_E3_left <- mm_style(p_E3_left, "Left Anti‑Feminist")
ggsave(file.path(fig_dir, "E3_left_anti_feminist_noninteracted.png"),
       fig_E3_left, width = 5, height = 5.4, dpi = 300)

# =========================
# Figure E4 — Feminism inconsistent: interacted (Ideology * Feminism)
# =========================

# Right Feminist: interacted
right_feminist2 <- right_feminist %>%
  mutate(profile1 = interaction(Ideology, Feminism, sep = "_"))

mm_E4_right <- mm(
  right_feminist2,
  choice_clean2 ~ profile1 + Ideology + Gender + Age + Feminism + Immigration + Crime,
  id = ~ numericalid
)
mm_E4_right$feature <- ifelse(mm_E4_right$feature == "profile1",
                              "Ideology*Feminism", mm_E4_right$feature)

p_E4_right <- plot(mm_E4_right[c(1,3,2,4), ], vline = 0.5) +
  scale_y_discrete(labels = c(
    "Left_Feminist"      = "Left and Feminist",
    "Left_Non-Feminist"  = "Left and Non‑Feminist",
    "Right_Feminist"     = "Right and Feminist",
    "Right_Non-Feminist" = "Right and Non‑Feminist"
  ))

fig_E4_right <- mm_style(p_E4_right, "Right Feminist (Interaction)")
ggsave(file.path(fig_dir, "E4_right_feminist_interacted.png"),
       fig_E4_right, width = 5, height = 5.4, dpi = 300)

# Left Anti‑Feminist: interacted
left_anti_fem2 <- left_anti_fem %>%
  mutate(profile1 = interaction(Ideology, Feminism, sep = "_"))

mm_E4_left <- mm(
  left_anti_fem2,
  choice_clean2 ~ profile1 + Ideology + Gender + Age + Feminism + Immigration + Crime,
  id = ~ numericalid
)
mm_E4_left$feature <- ifelse(mm_E4_left$feature == "profile1",
                             "Ideology*Feminism", mm_E4_left$feature)

p_E4_left <- plot(mm_E4_left[c(1,3,2,4), ], vline = 0.5) +
  scale_y_discrete(labels = c(
    "Left_Feminist"      = "Left and Feminist",
    "Left_Non-Feminist"  = "Left and Non‑Feminist",
    "Right_Feminist"     = "Right and Feminist",
    "Right_Non-Feminist" = "Right and Non‑Feminist"
  ))

fig_E4_left <- mm_style(p_E4_left, "Left Anti‑Feminist (Interaction)")
ggsave(file.path(fig_dir, "E4_left_anti_feminist_interacted.png"),
       fig_E4_left, width = 5, height = 5.4, dpi = 300)

# =========================
# End Appendix E
# =========================
